package info.mhylle.mediaplayer.playlist;

import helliker.id3.ID3v2FormatException;
import helliker.id3.MP3Comparator;
import helliker.id3.MP3File;
import helliker.id3.NoMPEGFramesException;
import info.mhylle.mediaplayer.data.DataManager;
import info.mhylle.mediaplayer.data.model.Album;
import info.mhylle.mediaplayer.data.model.Track;
import info.mhylle.mediaplayer.playlist.webitems.WebArtist;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Scope(org.jboss.seam.ScopeType.SESSION)
@Name("musiclist")
public class MusicList {
    private List<MusicAlbum> musicAlbums;
    private Playlist playlist;


    private MediaPlayer mediaPlayer;

    public MusicList() {
        System.out.println("new musiclist");
        musicAlbums = new ArrayList<MusicAlbum>();
        mediaPlayer = new MediaPlayer(this);
        DataManager manager = new DataManager();
        List<Album> albums = manager.getAlbums();

        for (Album album : albums) {
            Set<Track> set = album.getTracks();
            if (set.size() > 0) {
                MusicAlbum musicAlbum = new MusicAlbum();
                musicAlbum.setArtist(album.getArtistName());
                musicAlbum.setName(album.getAlbumName());
                musicAlbum.setReleaseYear(album.getReleaseyear());
                for (Track track : set) {
                    try {
                        MP3File mp3 = new MP3File(track.getPath());
                        musicAlbum.addTrack(mp3);
                    } catch (NoMPEGFramesException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (ID3v2FormatException e) {
                        e.printStackTrace();
                    }
                }
                musicAlbums.add(musicAlbum);
            }
        }
    }


    public void createMusicList(String directory) {
        MP3PlayListGenerator mp3PlayListGenerator = new MP3PlayListGenerator();
        try {
            File playlistdir = new File(directory);
            mp3PlayListGenerator.loadFromDirectory(playlistdir, true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<MusicAlbum> list = mp3PlayListGenerator.getAlbumList();

        for (MusicAlbum musicAlbum : list) {
            List<MP3File> tracks = musicAlbum.getTracks();
            if (tracks != null) {
                for (MP3File track : tracks) {
//                    try {
////                        System.out.println("   " + track.getTitle());
//                    } catch (ID3v2FormatException e) {
//                        e.printStackTrace();
//                    }
                }
            }
        }
    }

    public List<MusicAlbum> getAlbums() {
        return musicAlbums;
    }

    public List<WebArtist> getArtists() {
        List<WebArtist> result = new ArrayList<WebArtist>();
        for (MusicAlbum musicAlbum : musicAlbums) {
            WebArtist webArtist = new WebArtist();
            webArtist.setName(musicAlbum.getArtist());
            webArtist.setWebName(musicAlbum.getWebFriendlyArtistName());
            if (!result.contains(webArtist)) {
                result.add(webArtist);
            }
        }
        return result;
    }

    public List<MusicAlbum> getAlbumByArtist(String artistName) {
        List<MusicAlbum> result = new ArrayList<MusicAlbum>();

        for (MusicAlbum musicAlbum : musicAlbums) {
            if (artistName.equals(musicAlbum.getArtist())) {
                result.add(musicAlbum);
            }
        }
        return result;
    }

    public List<MP3File> getSongsByAlbum(String albumname, String albumartist) {
        List<MP3File> result = new ArrayList<MP3File>();

        for (MusicAlbum musicAlbum : musicAlbums) {
            if (musicAlbum.getTracks().size() > 0) {
                if (musicAlbum.getArtist().equals(albumartist)) {
                    if (musicAlbum.getName().equals(albumname)) {
                        List<MP3File> files = musicAlbum.getTracks();
                        for (MP3File file : files) {
                            result.add(file);
                        }
                        Collections.sort(result, new MP3Comparator());
                        return result;
                    }
                }
            }
        }
        return null;
    }

    public Playlist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(Playlist playlist) {
        this.playlist = playlist;
    }

    public void playPlaylist() {
        mediaPlayer.setPlaylist(playlist);
        mediaPlayer.playPlaylist();
    }

    public MediaPlayer getMediaPlayer() {
        return mediaPlayer;
    }

    public void setMediaPlayer(MediaPlayer mediaPlayer) {
        this.mediaPlayer = mediaPlayer;
        mediaPlayer.setMusicList(this);
    }
}
